﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;

namespace DALL.SysManage
{
   public class T_PUB_DEPARTMENT
    {

        /// <summary>
        /// 智能提示部门名称
        /// </summary>
        /// <returns></returns>
       public DataSet GetDepartmentName()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select DISTINCT DEPARTMENT_NAME FROM T_PUB_DEPARTMENT ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Model.SysManage.T_PUB_DEPARTMENT model)
        {
            Database db = DatabaseFactory.CreateDatabase();
            StringBuilder strSql = new StringBuilder();
            StringBuilder strSql1 = new StringBuilder();
            StringBuilder strSql2 = new StringBuilder();
            if (model.DEPARTMENT_NAME != null)
            {
                strSql1.Append("DEPARTMENT_NAME,");
                strSql2.Append("'" + model.DEPARTMENT_NAME + "',");
            }
            if (model.PARENTID != null)
            {
                strSql1.Append("PARENTID,");
                strSql2.Append("" + model.PARENTID + ",");
            }
            if (model.DESCRIPTION != null)
            {
                strSql1.Append("DESCRIPTION,");
                strSql2.Append("'" + model.DESCRIPTION + "',");
            }
            strSql.Append("insert into T_PUB_DEPARTMENT(");
            strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
            strSql.Append(")");
            strSql.Append(";select @@IDENTITY");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            int result;
            object obj = db.ExecuteScalar(dbCommand);
            if (!int.TryParse(obj.ToString(), out result))
            {
                return 0;
            }
            return result;
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(Model.SysManage.T_PUB_DEPARTMENT model)
        {
            Database db = DatabaseFactory.CreateDatabase();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_PUB_DEPARTMENT set ");
            if (model.DEPARTMENT_NAME != null)
            {
                strSql.Append("DEPARTMENT_NAME='" + model.DEPARTMENT_NAME + "',");
            }
            else
            {
                strSql.Append("DEPARTMENT_NAME= null ,");
            }
            if (model.PARENTID != null)
            {
                strSql.Append("PARENTID=" + model.PARENTID + ",");
            }
            else
            {
                strSql.Append("PARENTID= null ,");
            }
            if (model.DESCRIPTION != null)
            {
                strSql.Append("DESCRIPTION='" + model.DESCRIPTION + "',");
            }
            else
            {
                strSql.Append("DESCRIPTION= null ,");
            }
            int n = strSql.ToString().LastIndexOf(",");
            strSql.Remove(n, 1);
            strSql.Append(" where DEPARTMENT_ID=" + model.DEPARTMENT_ID + "");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.ExecuteNonQuery(dbCommand);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int DEPARTMENT_ID)
        {
            Database db = DatabaseFactory.CreateDatabase();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_PUB_DEPARTMENT ");
            strSql.Append(" where DEPARTMENT_ID=" + DEPARTMENT_ID + "");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            int rows = db.ExecuteNonQuery(dbCommand);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }		/// <summary>
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.SysManage.T_PUB_DEPARTMENT GetModel(int DEPARTMENT_ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select   ");
            strSql.Append(" DEPARTMENT_ID,DEPARTMENT_NAME,PARENTID,DESCRIPTION ");
            strSql.Append(" from T_PUB_DEPARTMENT ");
            strSql.Append(" where DEPARTMENT_ID=" + DEPARTMENT_ID + "");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            Model.SysManage.T_PUB_DEPARTMENT model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
            }
            return model;
        }
        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public List<Model.SysManage.T_PUB_DEPARTMENT> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select DEPARTMENT_ID,DEPARTMENT_NAME,PARENTID,DESCRIPTION ");
            strSql.Append(" FROM T_PUB_DEPARTMENT ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Model.SysManage.T_PUB_DEPARTMENT> list = new List<Model.SysManage.T_PUB_DEPARTMENT>();
            Database db = DatabaseFactory.CreateDatabase();
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
            }
            return list;
            
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select DEPARTMENT_ID,DEPARTMENT_NAME,PARENTID,DESCRIPTION ");
            strSql.Append(" FROM T_PUB_DEPARTMENT ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase();
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }
        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public Model.SysManage.T_PUB_DEPARTMENT ReaderBind(IDataReader dataReader)
        {
            Model.SysManage.T_PUB_DEPARTMENT model = new Model.SysManage.T_PUB_DEPARTMENT();
            object ojb;
            ojb = dataReader["DEPARTMENT_ID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.DEPARTMENT_ID = (int)ojb;
            }
            model.DEPARTMENT_NAME = dataReader["DEPARTMENT_NAME"].ToString();
            ojb = dataReader["PARENTID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.PARENTID = (int)ojb;
            }
            model.DESCRIPTION = dataReader["DESCRIPTION"].ToString();
            return model;
        }
    }
}
